package com.project.dhCamera.service.impl;

import com.project.dhCamera.entity.CommandTasker;
import com.project.dhCamera.service.TaskDao;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author Sweey Zhang
 * @description
 * @date 2021/7/20 12:02
 */
@Component
public class TaskDaoImpl implements TaskDao {
    // 存放任务信息
    private ConcurrentMap<String, CommandTasker> map = new ConcurrentHashMap<>(10);;

    @Override
    public CommandTasker get(String id) {
        return map.get(id);
    }

    @Override
    public Collection<CommandTasker> getAll() {
        return map.values();
    }

    @Override
    public int add(CommandTasker CommandTasker) {
        String id = CommandTasker.getId();
        if (id != null && !map.containsKey(id)) {
            map.put(CommandTasker.getId(), CommandTasker);
            if(map.get(id)!=null)
            {
                return 1;
            }
        }
        return 0;
    }

    @Override
    public int remove(String id) {
        if(map.remove(id) != null){
            return 1;
        };
        return 0;
    }

    @Override
    public int removeAll() {
        int size = map.size();
        try {
            map.clear();
        } catch (Exception e) {
            return 0;
        }
        return size;
    }

    @Override
    public boolean isHave(String id) {
        return map.containsKey(id);
    }
}
